Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
color-support
Advanced tools
A module which will endeavor to guess your terminal's level of color support.
The color-support package is designed to help developers determine the level of color support available in a given environment. This can be particularly useful for CLI tools or applications that output to the terminal, allowing them to adapt their color usage based on the capabilities of the terminal. It checks various factors, such as environment variables and terminal capabilities, to assess the level of color support.
Checking color support
This code demonstrates how to use the color-support package to check if the current environment supports color. The `colorSupport()` function returns a boolean indicating whether color is supported.
const colorSupport = require('color-support');
if (colorSupport()) {
console.log('Color is supported!');
} else {
console.log('Color is not supported.');
}
Determining level of color support
This code snippet shows how to determine the level of color support in the current environment. The `level` property can be used to adapt the output based on the capabilities of the terminal.
const colorSupport = require('color-support');
const level = colorSupport().level;
console.log(`Color support level: ${level}`);
Chalk is a popular npm package for styling terminal strings. Unlike color-support, which is focused on detecting color capabilities, Chalk provides a wide range of methods for applying styles (colors, background colors, bold, italic, etc.) to strings that are output to the terminal. Chalk automatically detects color support and gracefully degrades if necessary.
supports-color is another npm package that, similar to color-support, checks if the terminal supports color. However, supports-color provides more detailed information about the specific types of color support (e.g., 16 colors, 256 colors, TrueColor) and allows for more granular control and detection based on the environment and flags.
A module which will endeavor to guess your terminal's level of color support.
This is similar to supports-color
, but it does not read
process.argv
.
If not in a node environment, not supported.
If stdout is not a TTY, not supported, unless the ignoreTTY
option is set.
If the TERM
environ is dumb
, not supported, unless the
ignoreDumb
option is set.
If on Windows, then support 16 colors.
If using Tmux, then support 256 colors.
Handle continuous-integration servers. If CI
or
TEAMCITY_VERSION
are set in the environment, and TRAVIS
is not
set, then color is not supported, unless ignoreCI
option is set.
Guess based on the TERM_PROGRAM
environ. These terminals support
16m colors:
iTerm.app
version 3.x supports 16m colors, below support 256MacTerm
supports 16m colorsApple_Terminal
supports 256 colorsMake a guess based on the TERM
environment variable. Any
xterm-256color
will get 256 colors. Any screen, xterm, vt100,
color, ansi, cygwin, or linux TERM
will get 16 colors.
If COLORTERM
environment variable is set, then support 16 colors.
At this point, we assume that color is not supported.
var testColorSupport = require('color-support')
var colorSupport = testColorSupport(/* options object */)
if (!colorSupport) {
console.log('color is not supported')
} else if (colorSupport.has16m) {
console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m')
} else if (colorSupport.has256) {
console.log('\x1b[38;5;119m256 colors\x1b[0m')
} else if (colorSupport.hasBasic) {
console.log('\x1b[31mbasic colors\x1b[0m')
} else {
console.log('this is impossible, but colors are not supported')
}
If you don't have any options to set, you can also just look at the flags which will all be set on the test function itself. (Of course, this doesn't return a falsey value when colors aren't supported, and doesn't allow you to set options.)
var colorSupport = require('color-support')
if (colorSupport.has16m) {
console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m')
} else if (colorSupport.has256) {
console.log('\x1b[38;5;119m256 colors\x1b[0m')
} else if (colorSupport.hasBasic) {
console.log('\x1b[31mbasic colors\x1b[0m')
} else {
console.log('colors are not supported')
}
You can pass in the following options.
isTTY
check.TERM=dumb
environ check.CI
environ check.process.env
.isTTY
check. Defaults to process.stdout
.TERM
checking. Defaults to env.TERM
.false
).alwaysReturn
to return an object for level 0, all other
options are ignored, since no checking is done if a level is
explicitly set.If no color support is available, then false
is returned by default,
unless the alwaysReturn
flag is set to true
. This is so that the
simple question of "can I use colors or not" can treat any truthy
return as "yes".
Otherwise, the return object has the following fields:
level
- A number from 0 to 3
0
- No color support1
- Basic (16) color support2
- 256 color support3
- 16 million (true) color supporthasBasic
- Booleanhas256
- Booleanhas16m
- BooleanYou can run the color-support
bin from the command line which will
just dump the values as this module calculates them in whatever env
it's run. It takes no command line arguments.
This is a spiritual, if not actual, fork of supports-color by the ever prolific Sindre Sorhus.
FAQs
A module which will endeavor to guess your terminal's level of color support.
The npm package color-support receives a total of 8,719,667 weekly downloads. As such, color-support popularity was classified as popular.
We found that color-support demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.